home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 15 / macformat_15.iso / Shareware Internet / Desarrolladores / gray image 2.1 / vrectangle.cc < prev    next >
Text File  |  1995-05-30  |  12KB  |  346 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *               Grayscale Image
  6.  *
  7.  *      Verify the class Rectangle member functions and friends
  8.  *
  9.  * $Id: vrectangle.cc,v 2.0 1995/03/06 20:08:49 oleg Exp oleg $
  10.  *
  11.  ************************************************************************
  12.  */
  13.  
  14. #include "image.h"
  15. #include "std.h"
  16. #include <minmax.h>
  17. #include <iostream.h>
  18.  
  19. static IMAGE Test_image(16,32,8); //(256,512,8);
  20.  
  21. /*
  22.  *----------------------------------------------------------------------
  23.  *                   Image - Rectangle conversions
  24.  */
  25.  
  26. static void verify_image_rectangle_conversions(void)
  27. {
  28.   cout << "\nVerify Rectangle to image conversions\n";
  29.  
  30.   const int pattern = 145;
  31.  
  32.   {
  33.     cout << "\tVerify the clipped image has now nothing to do with the "
  34.             " parent\n";
  35.     Test_image = pattern;
  36.     IMAGE im1 = Test_image.square_of(min(Test_image.q_nrows(),
  37.                      Test_image.q_ncols()),rowcol(0,0));
  38.     im1 += 1;
  39.     assert( Test_image == pattern );
  40.  
  41.     IMAGE im2 ((Rectangle) Test_image);
  42.     assert( im2 == Test_image );
  43.  
  44.     im2 += 1;
  45.     assert( !(im2 == Test_image) );
  46.     assert( Test_image == pattern );
  47.  
  48.     IMAGE im3 = Test_image.rectangle(rowcol(0,0),
  49.                      rowcol(Test_image.q_nrows()-1,
  50.                         Test_image.q_ncols()-1));
  51.     assert( im3 == Test_image );
  52.     Test_image -= 1;
  53.     assert( !(im2 == Test_image) );
  54.     assert( Test_image == pattern-1 );
  55.     assert( im3 == pattern );
  56.   }
  57.  
  58.   {
  59.     cout << "\tCheck two different clippings from the image\n";
  60.     Test_image = pattern;
  61.     const int size = min(Test_image.q_nrows(),Test_image.q_ncols()) / 2;
  62.     if( size > 3 )
  63.     {
  64.       Test_image.square_of(size,rowcol(1,3)) += 5;
  65.       IMAGE im1(Test_image.rectangle(rowcol(0,2),rowcol(size-1,2+size-1)));
  66.       im1 += 5;
  67.       im1.rectangle(rowcol(1,1),rowcol(1+size-2,1+size-2)) -= 5;
  68.       IMAGE im2(Test_image.square_of(size,rowcol(1,3)));
  69.       assure( im1 == im2, "Image are different" );
  70.     }
  71.   }
  72.  
  73.   {
  74.     cout << "\tCheck clipping 'degenerated' rectangles\n";
  75.  
  76.     IMAGE im1 = Test_image.rectangle(rowcol(0,0),
  77.                      rowcol(0,Test_image.q_ncols()-1));
  78.     assert( im1.q_nrows() == 1 && im1.q_ncols() == Test_image.q_ncols() );
  79.  
  80.     IMAGE im2 = Test_image.rectangle(rowcol(0,1),
  81.                      rowcol(Test_image.q_nrows()-1,1));
  82.     assert( im2.q_ncols() == 1 && im2.q_nrows() == Test_image.q_nrows() );
  83.   }
  84.  
  85.   {
  86.     cout << "\tImage-to-image assignments considered as rectangles\n";
  87.     IMAGE im = Test_image;
  88.     im.clear();
  89.     Test_image = pattern;
  90.     assert( !(Test_image == im) );
  91.     (Rectangle)im = (Rectangle)Test_image;
  92.     assert( Test_image == im );
  93.     assert( im == pattern );
  94.   }
  95.  
  96.   cout << "\nDone\n\n";
  97. }
  98.  
  99.  
  100. /*
  101.  *----------------------------------------------------------------------
  102.  *           Testing operations on square blocks of images
  103.  */
  104.                     // Verify the pixels over the square
  105.                     // area have the value what is expected
  106. static void verify_pixel_value_over_square
  107.     (const int size, const rowcol rc, const GRAY val)
  108. {
  109.   register int i,j;
  110.   for(i=rc.row(); i<rc.row()+size; i++)
  111.     for(j=rc.col(); j<rc.col()+size; j++)
  112.       if( Test_image(i,j) != val )
  113.     _error("Pixel [%d,%d] has the value 0x%x different from expected 0x%x",
  114.            i,j,Test_image(i,j),val);
  115.  
  116.                     // Test the values beyond the 
  117.                     // square area boundaries.
  118.                     // They are supposed to be different
  119.   if( 
  120.      ( rc.row() + size < Test_image.q_nrows() &&
  121.        (i = rc.row()+size, j = rc.col()+size-1, Test_image(i,j) == val) ) ||
  122.      ( rc.col() + size < Test_image.q_ncols() &&
  123.        (i = rc.row()+size-1, j = rc.col()+size, Test_image(i,j) == val) ) ||
  124.      ( rc.row() > 0 &&
  125.        (i = rc.row()-1, j = rc.col(), Test_image(i,j) == val) ) ||
  126.      ( rc.col() > 0 &&
  127.        (i = rc.row(), j = rc.col()-1, Test_image(i,j) == val) )
  128.    )
  129.     _error("Pixel [%d,%d] beyond the square area has the same value 0x%x "
  130.        " as pixels within the area\n"
  131.        " The prev operation must have written beyond the square area",
  132.        i,j,val);
  133. }
  134.  
  135. static void test_pixel_op_over_square(const int size)
  136. {
  137.   const int pattern = 154;
  138.   register int i,j;
  139.  
  140.   cout << "\n\nTest reading/writing pixels and pixel modifications\n"
  141.           "over square blocks\n\n";
  142.   cout << "Size of the square block being tested " << size << "\n\n";
  143.  
  144.   Test_image.clear();
  145.  
  146.   cout << "\nBlock writing a pattern 0x" << hex << pattern << dec << "...\n";
  147.   cout << "and performing the entire set of tests "
  148.           "as for the whole image above ...";
  149.  
  150.   for(i=0; i<Test_image.q_nrows() - size; i+=5)
  151.     for(j=0; j<Test_image.q_ncols() - size; j+=3)
  152.     {
  153.       Test_image.sure_within(rowcol(i,j));
  154.       Test_image.square_of(size,rowcol(i,j)) = pattern;
  155.       verify_pixel_value_over_square(size,rowcol(i,j),pattern);
  156.  
  157.       double sum;
  158.       if( ( sum = sum_over(Test_image.square_of(size,rowcol(i,j))) ) !=
  159.       size*size*(double)pattern )
  160.     _error("Total sum of all the pixels over the square %g differs "
  161.            "from the expected value %g",sum,size*size*(double)pattern);
  162.  
  163.       Test_image.square_of(size,rowcol(i,j)) ^= ((1<<GRAY_MAXBIT)-1);
  164.       verify_pixel_value_over_square(size,rowcol(i,j),0xffff - pattern);
  165.  
  166.       const int test_val = 0x7e;
  167.       Test_image.square_of(size,rowcol(i,j)) = test_val;
  168.       verify_pixel_value_over_square(size,rowcol(i,j),test_val);
  169.  
  170.       Test_image.square_of(size,rowcol(i,j)) += 7;
  171.       verify_pixel_value_over_square(size,rowcol(i,j),test_val+7);
  172.       assure( Test_image >= 0, "Negative pixel unexpected");
  173.  
  174.       Test_image.square_of(size,rowcol(i,j)) -= 2*test_val;
  175.       verify_pixel_value_over_square(size,rowcol(i,j),test_val+7-2*test_val);
  176.       assure( !(Test_image > 0), "Non-Negative pixel unexpected");
  177.  
  178.       Test_image.square_of(size,rowcol(i,j)) -= -2*test_val+7;
  179.       verify_pixel_value_over_square(size,rowcol(i,j),test_val);
  180.  
  181.       Test_image.square_of(size,rowcol(i,j)) |= 0x03;
  182.       verify_pixel_value_over_square(size,rowcol(i,j),test_val | 0x03);
  183.  
  184.       Test_image.square_of(size,rowcol(i,j)) &= 0xff - 0x03;
  185.       verify_pixel_value_over_square(size,rowcol(i,j),test_val & (0xff-0x03));
  186.  
  187.       Test_image.square_of(size,rowcol(i,j)) ^= 0x02;
  188.       verify_pixel_value_over_square(size,rowcol(i,j),test_val);
  189.  
  190.       Test_image.square_of(size,rowcol(i,j)) *= 2;
  191.       verify_pixel_value_over_square(size,rowcol(i,j),2*test_val);
  192.  
  193.       Test_image.square_of(size,rowcol(i,j)) <<= 1;
  194.       verify_pixel_value_over_square(size,rowcol(i,j),4*test_val);
  195.  
  196.       Test_image.square_of(size,rowcol(i,j)) >>= 2;
  197.       verify_pixel_value_over_square(size,rowcol(i,j),test_val);
  198.  
  199.       Test_image.square_of(size,rowcol(i,j)) = 0;
  200.     }
  201.  
  202.   cout << "\nDone\n\n";
  203. }
  204.  
  205.  
  206. /*
  207.  *----------------------------------------------------------------------
  208.  *           Testing operations on rectangular blocks of images
  209.  */
  210.                     // Verify the pixels in the rectangle
  211.                     // have the value what is expected
  212. static void verify_pixel_value_over_rectangle
  213.     (const rowcol uppleft, const rowcol lowright, const GRAY val)
  214. {
  215.   register int i,j;
  216.   for(i=uppleft.row(); i<=lowright.row(); i++)
  217.     for(j=uppleft.col(); j<=lowright.col(); j++)
  218.       if( Test_image(i,j) != val )
  219.     _error("Pixel [%d,%d] has the value 0x%x different from expected 0x%x",
  220.            i,j,Test_image(i,j),val);
  221.  
  222.                     // Test the values beyond the 
  223.                     // rectangular area boundaries.
  224.                     // They ought to be different
  225.   rowcol rc(-1,-1);
  226.   if( 
  227.      ( lowright.row()+1 < Test_image.q_nrows() &&
  228.        Test_image(rc = lowright + rowcol(1,0)) == val ) ||
  229.      ( lowright.col()+1 < Test_image.q_ncols() &&
  230.        Test_image(rc = lowright + rowcol(0,1)) == val ) ||
  231.      ( uppleft.row() > 0 &&
  232.        Test_image(rc = uppleft - rowcol(1,0)) == val )  ||
  233.      ( uppleft.col() > 0 &&
  234.        Test_image(rc = uppleft - rowcol(0,1)) == val )
  235.    )
  236.     _error("Pixel [%d,%d] beyond the rectangular area has the same value 0x%x "
  237.        " as pixels within the area\n"
  238.        " The prev operation must have written beyond the square area",
  239.        rc.row(),rc.col(),val);
  240. }
  241.  
  242. static void test_pixel_op_over_rectangle(const int nrows, const int ncols)
  243. {
  244.   const int pattern = 104;
  245.   register int i,j;
  246.  
  247.   cout << "\n\nTest reading/writing pixels and pixel modifications\n"
  248.           "over rectangular blocks " << nrows << " by " << ncols << "\n\n";
  249.  
  250.   Test_image.clear();
  251.  
  252.   cout << "\nBlock writing a pattern 0x" << hex << pattern << dec << "...\n";
  253.   cout << "and performing the entire set of tests "
  254.           "as for the whole image in vimage ...";
  255.  
  256.   for(i=0; i<Test_image.q_nrows() - nrows; i+=5)
  257.     for(j=0; j<Test_image.q_ncols() - ncols; j+=3)
  258.     {
  259.       rowcol uppleft(i,j), lowright(i+nrows-1,j+ncols-1);
  260.       Test_image.sure_within(uppleft);
  261.       Test_image.sure_within(lowright);
  262.       Test_image.rectangle(uppleft,lowright) = pattern;
  263.       verify_pixel_value_over_rectangle(uppleft,lowright,pattern);
  264.  
  265.       double sum;
  266.       if( ( sum = sum_over(Test_image.rectangle(uppleft,lowright)) ) !=
  267.       nrows*ncols*(double)pattern )
  268.     _error("Total sum of all the pixels over the rectangle %g differs "
  269.            "from the expected value %g",sum,nrows*ncols*(double)pattern);
  270.  
  271.       Test_image.rectangle(uppleft,lowright) ^= ((1<<GRAY_MAXBIT)-1);
  272.       verify_pixel_value_over_rectangle(uppleft,lowright,0xffff - pattern);
  273.  
  274.       const int test_val = 0x7e;
  275.       Test_image.rectangle(uppleft,lowright) = test_val;
  276.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  277.  
  278.       Test_image.rectangle(uppleft,lowright) += 7;
  279.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val+7);
  280.       assure( Test_image >= 0, "Negative pixel unexpected");
  281.  
  282.       Test_image.rectangle(uppleft,lowright) -= 2*test_val;
  283.       verify_pixel_value_over_rectangle(uppleft,lowright,
  284.                     test_val+7-2*test_val);
  285.       assure( !(Test_image > 0), "Non-Negative pixel unexpected");
  286.  
  287.       Test_image.rectangle(uppleft,lowright) -= -2*test_val+7;
  288.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  289.  
  290.       Test_image.rectangle(uppleft,lowright) |= 0x03;
  291.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val | 0x03);
  292.  
  293.       Test_image.rectangle(uppleft,lowright) &= 0xff - 0x03;
  294.       verify_pixel_value_over_rectangle(uppleft,lowright,
  295.                     test_val & (0xff-0x03));
  296.  
  297.       Test_image.rectangle(uppleft,lowright) ^= 0x02;
  298.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  299.  
  300.       Test_image.rectangle(uppleft,lowright) *= 2;
  301.       verify_pixel_value_over_rectangle(uppleft,lowright,2*test_val);
  302.  
  303.       Test_image.rectangle(uppleft,lowright) <<= 1;
  304.       verify_pixel_value_over_rectangle(uppleft,lowright,4*test_val);
  305.  
  306.       Test_image.rectangle(uppleft,lowright) >>= 2;
  307.       verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  308.  
  309.       {
  310.     IMAGE im(Test_image);
  311.     im.rectangle(uppleft,lowright) = test_val;
  312.     Test_image.rectangle(uppleft,lowright) = 0;
  313.     Test_image.rectangle(uppleft,lowright) = 
  314.       im.rectangle(uppleft,lowright);
  315.     verify_pixel_value_over_rectangle(uppleft,lowright,test_val);
  316.       }    
  317.     
  318.       Test_image.rectangle(uppleft,lowright) = 0;
  319.     }
  320.  
  321.   cout << "\nDone\n\n";
  322. }
  323.  
  324. /*
  325.  *------------------------------------------------------------------------
  326.  *            The testing routing module
  327.  */
  328.  
  329. main()
  330. {
  331.   verify_image_rectangle_conversions();
  332.  
  333.   test_pixel_op_over_square(Test_image.q_nrows());
  334.   test_pixel_op_over_square(Test_image.q_nrows()/3);
  335.   test_pixel_op_over_square(1);
  336.  
  337.   test_pixel_op_over_rectangle(Test_image.q_nrows(),Test_image.q_ncols());
  338.   int size = min(Test_image.q_nrows(),Test_image.q_ncols());
  339.   size = ( size > 2 ? size / 2 : size );
  340.   test_pixel_op_over_rectangle(size,size);
  341.   test_pixel_op_over_rectangle((size == 4 ? 5 : 4),size);
  342.   test_pixel_op_over_rectangle(1,size);
  343.   test_pixel_op_over_rectangle(size,1);
  344. }
  345.  
  346.